package han.bashe.web.servlet;

import han.bashe.business.ejb.Pipeline;
import han.bashe.common.converter.HttpConverter;
import han.bashe.common.entity.BasheEntityConstants;
import han.bashe.common.exception.BasheErrorCode;
import han.bashe.common.exception.BasheException;
import han.bashe.common.message.NormalizedMessage;
import han.bashe.common.message.RequestHelper;
import han.bashe.common.util.ExceptionUtils;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import v1.message.bashe.han.BasheErrorDocument;

/**
 * Servlet implementation class for Servlet: HttpServlet
 *
 */
 public class HttpServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
   @EJB
   private Pipeline pipeline;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public HttpServlet() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			NormalizedMessage msg = HttpConverter.toNormalized(request);
			RequestHelper rh = new RequestHelper(msg);
			rh.setOrginMsgType(BasheEntityConstants.HTTP_TYPE);
			msg = pipeline.process(msg);
			System.out.println("response is \n" + msg);
			HttpConverter.toHttpResponse(msg, response);
			
		} catch (BasheException e) {
			Log log = LogFactory.getLog(HttpServlet.class);
			log.error("HttpServlet.doPost() error",e);
			BasheErrorDocument error = BasheErrorDocument.Factory.newInstance();
			error.addNewBasheError();
			error.getBasheError().setErrorCode(e.getCode());
			error.getBasheError().setErrorMsg(e.getMsg());
			error.getBasheError().setErrorTrace(ExceptionUtils.getTraceAsString(e));
			ServletReqResHeler.setResponseString(response, error.toString());
			
		}catch (Exception e) {
			Log log = LogFactory.getLog(HttpServlet.class);
			log.error("HttpServlet.doPost() error",e);
			BasheErrorDocument error = BasheErrorDocument.Factory.newInstance();
			error.addNewBasheError();
			error.getBasheError().setErrorCode(BasheErrorCode.UNKNOWN_ERROR);
			error.getBasheError().setErrorMsg(e.getMessage());
			error.getBasheError().setErrorTrace(ExceptionUtils.getTraceAsString(e));
			ServletReqResHeler.setResponseString(response, error.toString());
			
		}
		
	}   	  	    
}